﻿using B.S.DataBase.MyProject.Domain.productionworkorder;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SCWorkOrderCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SCWorkOrderCommandHandler
{ /// <summary>
  /// 生产工单的操作记录的查询命令处理方法
  /// </summary>
    public class SCJLQueryCommandHandler:IRequestHandler<SCJLQueryCommand,APIResult<List<CaoZuoJiLu>>>
    {
        /// <summary>
        /// 操作记录的仓储接口
        /// </summary>
        private readonly IBaseRepository<CaoZuoJiLu> cRepo;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="cRepo"></param>
        public SCJLQueryCommandHandler(IBaseRepository<CaoZuoJiLu>cRepo)
        {
            this.cRepo = cRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<CaoZuoJiLu>>> Handle(SCJLQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<CaoZuoJiLu>> result = new APIResult<List<CaoZuoJiLu>>();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            var list = cRepo.GetAll().Result;
            if(!string.IsNullOrEmpty(request.CaoName))
            {
                list = list.Where(x=>x.CaoName.Contains( request.CaoName));
            }
            list = list.OrderBy(x => x.JLId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Count = list.Count();    

            result.data = list.ToList();

            return await Task.FromResult(result);




        }
    }

}
